﻿using System.Collections.Generic;
using System.Data.Common;
using System.Linq;

namespace PropertyExpression.ControllableQuery.Toolkit
{
    public class CheckQueryContext
    {
        private readonly HashSet<string> hashSet = new HashSet<string>();
        public bool IsAlreadyChecked(string queryText, DbCommand command)
        {
            return hashSet.Contains(GetKey(queryText, command));
        }

        public void AddQueryText(string queryText, DbCommand command)
        {
            hashSet.Add(GetKey(queryText, command));
        }

        private static string GetKey(string queryText, DbCommand command)
        {
            return queryText +
                string.Join("", command.Parameters.Cast<DbParameter>().Select(_ => _.ParameterName + _.DbType)) +
                command.CommandType;
        }
    }
}